home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Learn C++ (CodeWarrior) / Chap 09.04 - virtual / virtual.cp < prev    next >
Text File  |  1995-10-21  |  1KB  |  81 lines

  1. #include <iostream.h>
  2.  
  3.  
  4. //---------------------------------------  Root
  5.  
  6. class Root
  7. {
  8.     protected:
  9.         short    num;
  10.         
  11.     public:
  12.                 Root( short numParam );
  13. };
  14.  
  15. Root::Root( short numParam )
  16. {
  17.     num = numParam;
  18.     
  19.     cout << "Root constructor called\n";
  20. }
  21.  
  22.  
  23. //---------------------------------------  Base1
  24.  
  25. class Base1 : public virtual Root
  26. {
  27.     public:
  28.         Base1();
  29. };
  30.  
  31. Base1::Base1() : Root( 1 )
  32. {
  33.     cout << "Base1 constructor called\n";
  34. }
  35.  
  36.  
  37. //---------------------------------------  Base2
  38.  
  39. class Base2 : public virtual Root
  40. {
  41.     public:
  42.         Base2();
  43. };
  44.  
  45. Base2::Base2() : Root( 2 )
  46. {
  47.     cout << "Base2 constructor called\n";
  48. }
  49.  
  50.  
  51. //---------------------------------------  Derived
  52.  
  53. class Derived : public Base1, public Base2
  54. {
  55.     public:
  56.                 Derived();
  57.         short    GetNum();
  58. };
  59.  
  60. Derived::Derived() : Root( 3 )
  61. {
  62.     cout << "Derived constructor called\n";
  63. }
  64.  
  65. short    Derived::GetNum()
  66. {
  67.     return( num );
  68. }
  69.  
  70.  
  71. //---------------------------------------  main()
  72.  
  73. int    main()
  74. {
  75.     Derived        myDerived;
  76.     
  77.     cout << "-------\n"
  78.         << "num = " << myDerived.GetNum();
  79.         
  80.     return 0;
  81. }